﻿using Book.Entities;
using Book.IRepositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace Book.Controllers
{
    [Route("api/article")]   
    [ApiController]
    [EnableCors("any")]
    public class ArticleController : ControllerBase
    {
        private readonly IArticleRepository _articleRepositiry;

        public ArticleController(IArticleRepository articleRepositiry)
        {
            this._articleRepositiry = articleRepositiry;
        }
        
        [HttpPost("add")]
        
        public IActionResult AddArticle([FromBody] JObject parameter)
        {
            string title = parameter.Value<string>("title");
            string content = parameter.Value<string >("content");
            string author = parameter.Value<string>("author");
            string menuid = parameter.Value<string>("menuid");
            string markdown = parameter.Value<string>("markdown");
            _articleRepositiry.AddArticle(title, content,author,menuid,markdown);
            return Ok();

        }
        [HttpGet("getarticlelistbymid")]
        public IActionResult GetArticleListByParentId(string menuid)
        {
            //int parentid = parameter.Value<int>("parentid");
            var rel= _articleRepositiry.GetArticleByParentId(menuid);
            return Ok(rel);
        }
        [HttpGet("getarticlebymid")]
        public IActionResult GetArticleByParentId(string menuid)
        {
            //int parentid = parameter.Value<int>("parentid");
            var rel =  _articleRepositiry.GetArticleByParentId(menuid);
            var res = rel.FirstOrDefault();
            return Ok(res);
        }
        [HttpGet("getarticlebyaid")]
        public IActionResult GetArticleByAId(string articleid)
        {
            
            var rel = _articleRepositiry.GetArticleByAId(articleid);
            
            return Ok(rel);
        }
        [HttpGet("getallarticle")]
        public IActionResult GetAllArticle()
        {

            var rel = _articleRepositiry.GetAllArticle();

            return Ok(rel);
        }
        [HttpGet("getallnews")]
        public IActionResult GetAllNews()
        {

            var rel = _articleRepositiry.GetAllNews();

            return Ok(rel);
        }
        [HttpPost("deletearticle")]
        public IActionResult DeleteArticle([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            _articleRepositiry.DeleteArticle(id);
            return Ok();
        }
        [HttpPost("updatearticle")]
        public IActionResult UpdateArticle([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            string content = parameter.Value<string>("content");         
            string title = parameter.Value<string>("title");
            string author = parameter.Value<string>("author");
            string markdown = parameter.Value<string>("markdown");
            _articleRepositiry.UpdateArticle(id, title, author, content, markdown);
            return Ok();
        }
    }
}
